package cn.finyzq.ss.games.service.impl;

import cn.finyzq.ss.games.persist.UserMapper;
import cn.finyzq.ss.games.service.UserService;
import cn.finyzq.ss.games.utils.ResultCode;
import cn.finyzq.ss.games.vo.ResultBean;
import cn.finyzq.ss.games.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create Time: 2018年08月12日 14:59
 *
 * @author fin
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 根据openid查询用户信息
     *
     * @param openid 用户标识
     * @return 用户信息
     */
    @Override
    public ResultBean<User> findUserByOpenid(String openid) {
        User user = userMapper.findUserByOpenid(openid);
        if(user != null){
            return new ResultBean<>(user);
        }else{
            return new ResultBean<>(ResultCode.USER_NOT_FOUND,"该用户不存在");
        }

    }

    /**
     * 添加用户信息
     *
     * @param user 用户数据
     * @return 添加结果
     */
    @Override
    public ResultBean addUser(User user) {
        int i = userMapper.insertSelective(user);
        if(i == 1){
            return new ResultBean();
        }else{
            return new ResultBean(ResultCode.INSERT_USER_FAIL, "用户授权失败");
        }
    }
}
